<?php
/**
 * actionclassdemo.php
 * This action shows how to use encapsulate an action in a class.  This has
 * the advantage of better organizing the code and avoiding namespace
 * collisions.  The only disadvantage I can think of is that it may not be
 * compatible with PHP 4.  But then that may be another feature.
 *
 * References
 *      http://klenwell.com/is/ProjectWikka
 *      http://docs.wikkawiki.org/UsingActions
 *
 * @package     Action
 * @author      Tom Atwell <klenwell@gmail.com>
 * @copyright   Copyright 2010, Tom Atwell <klenwell@gmail.com>
 * @license     http://www.gnu.org/copyleft/gpl.html GNU General Public License
 *
 * @uses        Wakka::LoadSingle()
 * @uses        Wakka::GetConfigValue()
 *
 * @input       str $foo optional: a sample action parameter
 * @input       bool $show_code optional: whether or not to show debug output
 * @output      last page updated (from database)
 * @output      Debug info, including print out of this file
 */

class WikkaDemoAction {
    # defaults

    # internal
    var $Wikka = NULL;
    var $Param = array();
    var $user = NULL;
    var $user_role = 'viewer';
    var $is_admin = FALSE;
    var $format = 'wikka';
    var $datestampf = 'Y-m-d H:i:s';

    function __construct($Wakka=NULL, $ActionParams=array()) {
        if ( is_null($Wakka) ) {
            $this->_abort('must pass wakka object as argument for action class');
        }
        $this->Wikka = $Wakka;
        $this->_load_wikka_acl();
        $this->Param = $ActionParams;
        $this->Get = $_GET;
        $this->Post = $_POST;
        $this->page = $this->Wikka->tag;

        # get here
        if ( isset($_SERVER['SCRIPT_URL']) ) {
            $this->here = $_SERVER['SCRIPT_URL'];
        }
        elseif ( isset($_SERVER['REQUEST_URI']) ) {
            $SplitUri = explode('?', $_SERVER['REQUEST_URI']);
            $this->here = $SplitUri[0];
        }
        else {
            $this->here = $this->Wikka->GetConfigValue('base_url') . $this->page;
        }
    }

    function main() {
        $LastUpdate = $this->_get_last_page_update();
        return $this->_build_output($LastUpdate);
    }

    function output($content, $wikka_format=0) {
        if ( $wikka_format ) {
            $content = $this->wikka->format($content);
        }
        print $content;
    }

    function _get_last_page_update() {
        $sql_f = <<<HSQL
SELECT P.tag, P.time, P.latest, TIMEDIFF(Now(), P.time) as age,
    P.user, TIMEDIFF(Now(), U.signuptime) as user_age,
    Now() as now
FROM %s%s as P
LEFT JOIN %s%s as U on U.name = P.user
WHERE 1 = 1
    AND latest = 'Y'
ORDER BY time DESC, tag
LIMIT 1
HSQL;

        $sql = sprintf( $sql_f,
                        $this->Wikka->GetConfigValue('table_prefix'), 'pages',
                        $this->Wikka->GetConfigValue('table_prefix'), 'users' );

        return $this->Wikka->LoadSingle($sql);
    }

    function _build_output($LastPage) {
        $template = <<<XHTML
<div class="action_output" id="actiontemplate_output">
    <div class="status">
        <strong>Last Page Update:</strong>
        <span class="hilight">%s<span>
        at <span class="hilight">%s<span>
        by <span class="hilight">%s<span>
    </div>
    <div class="status">
        <strong>Your Status (viewer, user, or admin):</strong>
        <span class="hilight">%s<span>
    </div>
    <div class="status">
        <strong>action 'foo' parameter:</strong>
        <span class="hilight">%s<span>
    </div>
    <div class="actionbar">
        <a href="%s?show_code=1" class="%s">show code</a> |
        <a href="%s?show_code=0" class="%s">hide code</a>
    </div>
    <div style="display:%s;">
        <h4>Parameters</h4>
        <pre>%s</pre>

        <h4>Database Result</h4>
        <pre>%s</pre>

        <h4>Action Code</h4>
        <pre>%s</pre>
    </div>
</div>
XHTML;
        # content params
        $source_code = highlight_file(__FILE__, 1);
        $show_code = (isset($this->Get['show_code'])) ? $this->Get['show_code'] : 0;
        $show_code_class = ( $show_code ) ? 'active' : 'normal';
        $hide_code_class = ( ! $show_code ) ? 'active' : 'normal';
        $code_display = ( $show_code ) ? 'block' : 'none';
        $foo_param = (isset($this->Param['foo'])) ? $this->Param['foo'] : '(none)';

        # output
        $content = sprintf( $template,
                            $LastPage['tag'], $LastPage['time'], $LastPage['user'],
                            $this->user_role,
                            $foo_param,
                            $this->here, $show_code_class,
                            $this->here, $hide_code_class,
                            $code_display,
                            htmlentities(print_r($this->Param,1)),
                            print_r($LastPage,1),
                            $source_code );

        return $content;
    }

    function _load_wikka_acl() {
        $this->user = $this->Wikka->GetUser();
        if ( !empty($this->user) ) {
            $this->is_admin = $this->Wikka->IsAdmin($this->user);
        }

        if ( $this->is_admin ) {
            $this->user_role = 'admin';
        }
        elseif ( $this->user ) {
            $this->user_role = 'user';
        }

        return;
    }
}


# Main Routine
try {
    # note: must pass Wikka object ($this) and $vars
    $Action = new WikkaDemoAction($this, $vars);
    $content = $Action->main();
    $Action->output($content);
}
catch(Exception $e) {
    printf('Exception: %s', $e->getMessage());
}

?>
